package com.hc.baselibray.ioc;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ViewUtils {
    public  static  void  inject(Activity activity){
        inject(new ViewFinder(activity),activity);
    }
    //后期
    public  static  void  inject(View view){
        inject(new ViewFinder(view),view);
    }
    //后期
    public  static  void  inject(View view,Object object){
       inject(new ViewFinder(view),object);
    }
    //兼容上面三个方法 object->反射需要执行的类
    private  static  void inject(ViewFinder finder,Object object){
        injectFiled(finder,object);
        injectEvent(finder,object);
    }

//注入事件
    private static void injectEvent(ViewFinder finder, Object object) {
        //1获取类里面的所有方法
      Class<?> clazz =object.getClass();
        Method[] methods=clazz.getDeclaredMethods();

        //获取onclick里面的值
        for(Method method:methods){
            OnClick onClick=method.getAnnotation(OnClick.class);
            if(onClick!=null){
                int[] viewIds= onClick.value();
                for(int viewId:viewIds){
                    View view=finder.findViewById(viewId);

                    if(view!=null){
                        view.setOnClickListener(new DeclaredOnClickListener(method,object));
                    }
                }

            }

        }

    }
    private  static  class  DeclaredOnClickListener implements  View.OnClickListener{
        private final Object object;
        private final Method method;
        public DeclaredOnClickListener(Method method,Object object) {
            this.method=method;
            this.object=object;
        }

        @Override
        public void onClick(View view) {
            method.setAccessible(true);
            //拓展功能，检测网络
            boolean isCheckNet=method.getAnnotation(CheckNet.class)!=null;
            Log.d("isCheckNet", "onClick: "+isCheckNet);
            if(isCheckNet&&!isNetworkAvailable(view.getContext())){
                Toast.makeText(view.getContext(),"网络不给力，请检查",Toast.LENGTH_LONG).show();

            }
            else{
                try{
                    method.invoke(object,view);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 检查网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }
    //注入属性
    private static void injectFiled(ViewFinder finder, Object object) {
        //1。获取类里面的所有属性
        Class<?> clazz=object.getClass();
        //获取所有的属性，包括私有和共有
        Field[] fields=clazz.getDeclaredFields();

        //2。获取viewbyid里面的value值
        for(Field field:fields){
            ViewById viewById=field.getAnnotation(ViewById.class);
            if(viewById!=null){
                //获取注解里面的id值-》R.id.tv
                int viewId=viewById.value();
                //3。findviewbyid找到view
                View view=finder.findViewById(viewId);

                if(view!=null){
                    //能够注入所有的修饰符，private public
                    field.setAccessible(true);
                    //4。动态的注入找到的view
                    try{
                        field.set(object,view);
                    }catch (IllegalAccessException e){
                        e.printStackTrace();
                    }
                }


            }
        }



    }
}
